const app = getApp()
import {
  request
} from '../../utils/request.js'
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
Page({

	/**
	 * 页面的初始数据
	 */
  data: {
    name: '',
    tel: '',
    address: [],
    addressDes: '',
    isDefault: false,
    id: '',
    itemDesc: '',
    province: '',
    area: '',
    city: '',
    flag: 'add',
    id: '',
    desc: '',
    detail: ''
  },

	/**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id
      });
      this.getAddressById()
    } else {
      this.setData({
        address: ['河南省', '洛阳市']
      });
    }
  },
  getAddressById() {
    request({
      url: 'addressDetail',
      token: wx.getStorageSync('token'),
      method: 'POST',
      data: {
        id: this.data.id
      }
    }).then(res => {
      this.setData({
        desc: res.data.data
      });
    })
  },

  // 点击地图icon获取当前位置
  getMap() {
    var that = this
    wx.chooseLocation({
      success: function (res) {
        console.log(res)
        const latitude = res.latitude
        const longitude = res.longitude
        console.log(latitude)
        that.getAddress(that, latitude, longitude)
      }
    })




  },


  getAddress(that, latitude, longitude) {

    var qqmapsdk = new QQMapWX({
      key: 'F7JBZ-P24WS-J2SOX-6RKEL-A5NK3-JKFV7'
    });
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      // coord_type: 3,
      success: function (res) {
        console.log(res)
        const data = res.result
        that.setData({
          address: [data.ad_info.province, data.ad_info.city, data.ad_info.district],
          province: data.ad_info.province,
          city: data.ad_info.city,
          country: data.ad_info.district,
          detail: data.address_component.street + data.formatted_addresses.recommend,
        });
        console.log(that.data.detail)
      },
      fail: function (res) {
        console.log(res)
      }
    })
    console.log(latitude)
  },

  createAddress() {
    let data = this.data;
    let desc = this.data.desc;
    if (!data.name.trim()) {
      wx.showToast({
        title: '请输入收货人姓名',
        icon: 'none'
      });
      return
    } else if (!data.mobile) {
      wx.showToast({
        title: '请输入收货人手机号',
        icon: 'none'
      });
      return
    } else if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(data.mobile)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      });
      return
    } else if (!data.detail) {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none'
      });
      return
    }
    // if (this.data.id) {
    // 	if (!desc.province) {
    // 		wx.showToast({
    // 			title: '请选择所在地',
    // 			icon: 'none'
    // 		});
    // 		return
    // 	}
    // } else {
    // 	if (!this.data.address.length) {
    // 		wx.showToast({
    // 			title: '请选择所在地',
    // 			icon: 'none'
    // 		});
    // 		return
    // 	}
    // }
    request({
      url: 'createAddress',
      token: wx.getStorageSync('token'),
      method: 'POST',
      data: {
        name: data.name || desc.name,
        mobile: data.mobile || desc.mobile,
        province: data.province || desc.province,
        city: data.city || desc.city,
        country: data.country || desc.country,
        detail: data.detail || desc.detail
      }
    }).then(res => {
      console.log(res)
      wx.navigateBack({
        delta: 1
      });
    })
  },
  updateAddress() {
    let data = this.data;
    let desc = this.data.desc;
    request({
      url: 'updateAddress',
      token: wx.getStorageSync('token'),
      method: 'POST',
      data: {
        id: this.data.id,
        name: data.name || desc.name,
        mobile: data.mobile || desc.mobile,
        province: data.province || desc.province,
        city: data.city || desc.city,
        country: data.country || desc.country,
        detail: data.detail || desc.detail
      }
    }).then(res => {
      wx.navigateBack({
        delta: 1
      });
    })
  },


  bname(e) {
    this.setData({
      name: e.detail.value
    })
  },
  btel(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  baddressDes(e) {
    this.setData({
      detail: e.detail.value
    })
  },

  bindRegionChange: function (e) {
    this.setData({
      address: e.detail.value,
      province: e.detail.value[0],
      city: e.detail.value[1],
      country: e.detail.value[2]
    })
  },


	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function () {

  },

	/**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function () {

  },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function () {

  },

	/**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function () {

  },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function () {

  },

	/**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function () {

  },

	/**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function () {

  }
})
